package do1.mobo.widget;

import do1.common.json.JSONObject;
import android.content.Context;
import android.view.ViewGroup;
import android.widget.Spinner;
import do1.common.util.JsonUtil;
import do1.mobo.activity.R;
import do1.mobo.constant.Constant;
import do1.mobo.graphics.ImageFactory;

public class DropdownCtrl extends Spinner
{
	private ListViewAdapter adapter;
	
	private int fontColor;
	private int fontSize;
	
	public DropdownCtrl( Context cnText )
	{
		super(cnText);
	}
	
	//@Override
	public String getContentDescription()
	{	
		String value=null;
		ViewTag tag=(ViewTag)getSelectedView().getTag();
		if(tag!=null)
		{
			value=tag.getValue();
		}
		return value;
	}
	
	@Override 
	public void setContentDescription(CharSequence contentDescription)
	{
		adapter.clear();
		//String[] values=((String) contentDescription).split(",");
		//adapter.addListViewItemData(values,null,null,20,fontColor);
		adapter.addListViewItemData((String)contentDescription,null,null,20,fontColor);
		setAdapter(adapter);
	}
	
	public void parse( JSONObject json, ImageFactory imageFactory ,int parentWidth)
    { 
		BaseAttribute base=new BaseAttribute();
		base.parse(json);
		fontColor=base.getFontColor();
		fontSize=base.getFontSize();
		
		//width
		int width=BaseAttribute.getLayoutParamsWidth(json, parentWidth,ViewGroup.LayoutParams.FILL_PARENT);
		setLayoutParams(LayoutUtil.getLayoutParams(width , ViewGroup.LayoutParams.WRAP_CONTENT));
		
		//menu
		JSONObject comboMenuObj=JsonUtil.getJsonObject(json,Constant.JSON_KEY_MENU);
		if(comboMenuObj!=null)
		{
			adapter=new ListViewAdapter(getContext());
			adapter.setListItemLayout(R.layout.list_item_icon_text);
			adapter.specifyFontColor(fontColor);
			adapter.specifyFontSize(fontSize);
			adapter.parse(comboMenuObj, imageFactory);
			setAdapter(adapter);
		}
		
		//id
		String name=JsonUtil.getString(json, Constant.JSON_KEY_ID);
		
		//ViewTag
		ViewTag tag=new ViewTag(this);
		tag.setValueable(true);
		tag.setName(name);
		setTag(tag);
    }
}
